import axios from "axios";
import {MessageInstance} from "antd/es/message/interface";
import {message} from "antd";

const httpApi = axios.create()

let messageApi: MessageInstance = message;

httpApi.interceptors.request.use((config) => {
    return config
})

httpApi.interceptors.response.use((response) => {
    if (response.data.success == false) {
        messageApi.error(response.data.msg)
    }
    return response;
}, (error) => {
    if (error.response.state == 401) {
        return {
            success: false,
            msg: "未登录",
            data: null
        }
    }
})

export default httpApi;
export const setMessageApi = (a: MessageInstance) => {
    messageApi = a;
}
